package com.shenhaofeng.app.http.request;

import com.shenhaofeng.app.http.CacheControl;
import com.shenhaofeng.app.http.Headers;
import com.shenhaofeng.app.http.Method;
import com.shenhaofeng.app.http.url.HttpUrl;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 *
 */
public class Request {


    private String method;

    private HttpUrl httpUrl;

    private CacheControl cacheControl;

    //header数据集
    private Headers headers;

    private RequestBody body;


    private Request(String method, HttpUrl httpUrl, CacheControl cacheControl, Headers headers, RequestBody body) {
        this.method = method;
        this.httpUrl = httpUrl;
        this.cacheControl = cacheControl;
        this.headers = headers;
        this.body = body;
    }


    protected Request(Builder builder) {
        this.method = builder.method;
        this.httpUrl = builder.httpUrl;
        this.cacheControl = builder.cacheControl;
        this.headers = builder.headersBuilder.build();
        this.body = builder.requestBody;
    }


    public String method() {
        return method;
    }


    protected void method(String method, RequestBody body) {
        this.method = method;
        this.body = body;
    }


    public HttpUrl url() {
        return httpUrl;
    }


    protected void url(HttpUrl url) {
        this.httpUrl = url;
    }


    public void headers(Headers headers) {
        this.headers = headers;
    }


    public Headers headers() {
        return this.headers;
    }


    public void requestBody(RequestBody body) {
        this.body = body;
    }


    public RequestBody requestBody() {
        return body;
    }


    public CacheControl cacheControl() {
        return cacheControl;
    }


    public void cacheControl(CacheControl cacheControl) {
        this.cacheControl = cacheControl;
    }


    /**
     * 请求构建器
     */
    public static class Builder {

        private Map<String, File> files = new HashMap<>();

        private Map<String, String> kv = new HashMap<>();

        private Headers.Builder headersBuilder = new Headers.Builder();

        private HttpUrl httpUrl;

        private CacheControl cacheControl;

        private String method = Method.GET;

        private RequestBody requestBody;


        /**
         * 配置请求方法,默认为GET
         *
         * @param method
         * @return
         */
        public Builder method(String method, RequestBody requestBody) {
            this.method = method;
            this.requestBody = requestBody;

            return this;
        }


        public Builder header(String name, String value) {
            headersBuilder.put(name, value);
            return this;
        }


        /**
         * 请求方式为GET
         *
         * @return
         */
        public Builder get() {
            return method(Method.GET, null);
        }


        /**
         * 请求方式为POST
         *
         * @param requestBody
         * @return
         */
        public Builder post(RequestBody requestBody) {
            return method(Method.POST, requestBody);
        }


        public Builder post(Entry... entries) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            for (int i = 0; i < entries.length; i++) {
                Entry entry = entries[i];
                if (entry.isValue()) {
                    builder.addEntity(entry.getName(), entry.getValue());
                } else if (entry.isFile()) {
                    builder.addEntity(entry.getName(), entry.getFilename(), new FileBody.Builder()
                            .file(entry.getFile())
                            .mediaType(entry.getMediaType())
                            .build());
                }
            }
            return post(builder.build());
        }


        public Builder patch() {
            return method(Method.PATCH, requestBody);
        }


        public Builder head() {
            return method(Method.HEAD, null);
        }


        public Builder delete() {
            return delete(null);
        }


        public Builder delete(RequestBody requestBody) {
            return method(Method.DELETE, requestBody);
        }


        public Builder put(RequestBody requestBody) {
            return method(Method.PUT, requestBody);
        }


        /**
         * 配置请求地址
         *
         * @param url
         * @return
         */
        public Builder url(HttpUrl url) {
            this.httpUrl = url;
            return this;
        }


        public Builder cacheControl(CacheControl cacheControl) {
            this.cacheControl = cacheControl;
            return this;
        }


        /**
         * 构建请求对象
         *
         * @return
         */
        public Request build() {
            return new Request(this);
        }
    }


}
